<?php

namespace app\qsapi\controller;

use app\common\controller\Sms;
use app\qsapi\service\AuthorityService;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\facade\Env;
use think\response\Json;

/**
 * 登录权限
 */
class Authority extends BaseController
{
    protected $needLogin = ['phonemodifypassword'];
    protected $noNeedLogin = ['sendsms', 'register', 'login', 'loginByPhone', 'touchMobile',
        'touchMobuieUni', 'loginbyphone', 'touchmobuieuni', 'touchMobuieUniQn', 'touchmobuieuniqn', 'touchMobuieUniQnqy',
        'touchmobuieuniqnqy', 'transferSendSms', 'transfersendsms', 'resetpassword', 'checkphonecode', 'checkusernameandphone',
        'checkusername', 'touchmobuiechuang'];
    /**
     * 发送验证码
     * @return bool|string
     */
    public function sendSms(): Json
    {
        $authorityService = new AuthorityService();
        $phone = $this->request->post('phone') ?? '';
        $type = $this->request->post('type') ?? 1;
        return json($authorityService->sendSms($phone, $type));
    }

    /**
     * 用户名密码注册
     * @return Json
     */
    public function register(): Json
    {
        $param = $this->request->post();
        $authorityService = new AuthorityService();
        return json($authorityService->register($param));
    }

    /**
     * 账号密码登录
     * @return Json
     */
    public function login(): Json
    {
        $param = $this->request->post();
        $authorityService = new AuthorityService();
        return json($authorityService->loginByPass($param));

    }

    /**
     * 手机号验证码登录
     * @return Json
     */
    public function loginByPhone(): Json
    {
        $param = $this->request->post();
        $authorityService = new AuthorityService();
        return json($authorityService->loginByPhone($param));
    }


    /**
     * Desc : 验证当前用户是否存在
     * User : you name
     * Date : 2024-01-15 11:23
     */
    public function checkUsername(): Json
    {
        $param['username'] = $this->request->post('username') ?? '';
        $authorityService = new AuthorityService();
        return json($authorityService->checkUsername($param));
    }

    /**
     * Desc : 验证当前用户是否跟当前手机号匹配,并发送短信验证码
     * User : you name
     * Date : 2024-01-10 18:28
     * @return Json
     */
    public function checkUserNameAndPhone(): Json
    {
        $param['username'] = $this->request->post('username') ?? '';
        $param['phone'] = $this->request->post('phone') ?? '';
        $authorityService = new AuthorityService();
        return json($authorityService->checkUserNameAndPhone($param));
    }

    /**
     * Desc : 重新设置密码
     * User : you name
     * Date : 2024-01-10 19:11
     * @return Json
     */
    public function resetPassword(): Json
    {
        $param = $this->request->post();
        $authorityService = new AuthorityService();
        return json($authorityService->resetPassword($param));
    }

    /**
     * Desc : 验证验证码是否正确
     * User : you name
     * Date : 2024-01-10 19:36
     * @return Json
     */
    public function checkPhoneCode(): Json
    {
        $authorityService = new AuthorityService();
        return json($authorityService->checkPhoneCode($this->request->post('phone'), $this->request->post('code'), $this->request->post('username')));
    }

    /**
     * APP端一键登录 创蓝
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function touchMobuieChuang(): Json
    {
        $token = $this->request->post('token', '');//App端SDK获取的登录Token
        $device = $this->request->post('device', '');//App端获取用户设备号
        $data = $this->request->param();
        $authorityService = new AuthorityService();
        return json($authorityService->touchMobuieChuang($token, $device, $data));
    }

    /**
     * Desc : 盒子侧补充登录日志
     * User : steven
     * Date : 2024-03-22 16:24
     * @return Json
     */
    public function addLoginLog(): Json
    {
        $authorityService = new AuthorityService();
        $member_id = $this->request->getMemberId();
        $device_params = $this->request->post();
        $authorityService->addLoginLog($member_id, get_client_ip(), $device_params);
        return json(['data' => [], 'msg' => 'success', 'code' => 200]);
    }

    /**
     * Desc : 手机号修改密码
     * User : Steven
     * Date : 2024-07-29 10:02
     */
    public function phoneModifyPassword(): Json
    {
        $authorityService = new AuthorityService();
        $member_id = $this->request->getMemberId() ?? 0;
        return json($authorityService->phoneModifyPassword($member_id, $this->request->post()));
    }

    /**
     * Desc : 原密码修改密码
     * User : Steven
     * Date : 2024-07-29 10:53
     * @return Json
     */
    public function passwordModify(): Json
    {
        $authorityService = new AuthorityService();
        $member_id = $this->request->getMemberId();
        return json($authorityService->passwordModify($member_id, $this->request->post()));
    }

    /**
     * Desc : 账号注册绑定手机号
     * User : Steven
     * Date : 2024-07-29 14:48
     * @return Json
     */
    public function bindMobile(): Json
    {
        $authorityService = new AuthorityService();
        $member_id = $this->request->getMemberId();
        return json($authorityService->bindMobile($member_id,$this->request->post()));
    }

}
